package com.motong.gongdan.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.motong.gongdan.entity.DeviceStatus;
import com.motong.gongdan.mapper.DeviceStatusMapper;
import com.motong.gongdan.vo.system.device.query.StatusDashboardQuery;
import com.motong.gongdan.vo.system.device.query.StatusLogQuery;
import com.motong.gongdan.vo.system.device.vo.DeviceStatusRateVo;
import com.motong.gongdan.vo.system.device.vo.StatusDashboardVo;
import com.motong.gongdan.vo.system.lampRequest.query.LampTimingQuery;
import com.motong.gongdan.vo.system.lampRequest.vo.LampTimingDeviceStatusRateVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeviceStatusService extends ServiceImpl<DeviceStatusMapper, DeviceStatus> {

    @Resource
    private DeviceStatusMapper deviceStatusMapper;

    public StatusDashboardVo statusDashboard(StatusDashboardQuery query) {
        List<DeviceStatusRateVo> listVo = deviceStatusMapper.deviceStatusRate(query);

        StatusDashboardVo statusDashboardVo = new StatusDashboardVo().setDeviceStatusRateVos(listVo);
        Map<String, Integer> typeCountMap = new LinkedHashMap<>(5);
        typeCountMap.put("离线", 0);
        typeCountMap.put("故障", 0);
        typeCountMap.put("等待", 0);
        typeCountMap.put("运行", 0);
        if (query.showClose) {
            typeCountMap.put("关机", 0);
        }
        //数量统计
        query.setDeviceStatusList(null);
        List<DeviceStatusRateVo> allListVo = deviceStatusMapper.deviceStatusRate(query);
        allListVo.forEach(i -> typeCountMap.computeIfPresent(i.getStatusName(), (k, v) -> ++v));
        statusDashboardVo.setStatusCountMap(typeCountMap);
        // 获取语音播报信息
        List<String> deviceIds = listVo.stream().map(DeviceStatusRateVo::getDeviceId).collect(Collectors.toList());
        return statusDashboardVo;

    }

    public List<LampTimingDeviceStatusRateVo> deviceStatusRate(LampTimingQuery query) {
        StatusDashboardQuery statusDashboardQuery = new StatusDashboardQuery().setDeviceId(query.getDeviceId()).setStartTime(query.getStartTime()).setEndTime(query.getEndTime()).setShowOffline(false);
        BigDecimal total = deviceStatusMapper.lampRateInfo(statusDashboardQuery).stream().map(DeviceStatusRateVo::getDuration).reduce(BigDecimal::add).orElse(BigDecimal.ONE);
        List<LampTimingDeviceStatusRateVo> data = deviceStatusMapper.lampRateInfo(statusDashboardQuery)
                .stream().map(i -> new LampTimingDeviceStatusRateVo()
                        .setStatusName(i.getStatusName()).setRate(i.getDuration().divide(total, 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)).toString())
                        .setDurationHour(i.getDuration().longValue() / 60 / 60)).collect(Collectors.toList());
        return data;
    }

    public Page<DeviceStatus> statusLog(Page<DeviceStatus> page, StatusLogQuery query) {
        return this.deviceStatusMapper.statusLog(page,query);
    }
}
